/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.psp.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.cspimp.psp.domain.Chemicla;
import me.zhengjie.modules.cspimp.psp.service.ChemiclaService;
import me.zhengjie.modules.cspimp.psp.service.dto.ChemiclaQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://docs.auauz.net
* @author zmm
* @date 2020-05-30
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "化学品管理管理")
@RequestMapping("/api/chemicla")
public class ChemiclaController {

    private final ChemiclaService chemiclaService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('chemicla:list')")
    public void download(HttpServletResponse response, ChemiclaQueryCriteria criteria) throws IOException {
        chemiclaService.download(chemiclaService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询化学品管理")
    @ApiOperation("查询化学品管理")
    @PreAuthorize("@el.check('chemicla:list')")
    public ResponseEntity<Object> query(ChemiclaQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(chemiclaService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增化学品管理")
    @ApiOperation("新增化学品管理")
    @PreAuthorize("@el.check('chemicla:add')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chemicalsCn", value = "化学品中文名称",dataType = "String",required = true,paramType = "body"),
            @ApiImplicitParam(name = "chemicalsEn", value = "化学品英文名称",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsAlias", value = "化学品别名",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsCas", value = "cas",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsType", value = "化学品类别",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsMsds", value = "化学品文件",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "supplierName", value = "供应商名称",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "supplierContactName", value = "供应商联系人",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "supplierContactPhone", value = "供应商联系方式",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "emConsultantName", value = "咨询单位名称",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "emContactName", value = "应急联系人",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "emContactPhone", value = "应急联系方式",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "storageArea", value = "仓库区域",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "useArea", value = "使用区域",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "isDangerousGood", value = "是否是危险品",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "dangerousCategory", value = "危险类型",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "registrationNo", value = "登记号",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "packingCategory", value = "包装类别",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "hazardCharacteristics", value = "危险特性",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "activeHazard", value = "活性危害",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "prevention", value = "预防及应急措施",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "hazardNo", value = "危规号",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "incompatibility", value = "禁配物",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "stockCount", value = "库存量",dataType = "String",paramType = "body")

    })
    public ResponseEntity<Object> create(@Validated @RequestBody Chemicla resources){
        return new ResponseEntity<>(chemiclaService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改化学品管理")
    @ApiOperation("修改化学品管理")
    @PreAuthorize("@el.check('chemicla:edit')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id",dataType = "Long",required = true,paramType = "body"),
            @ApiImplicitParam(name = "chemicalsCn", value = "化学品中文名称",dataType = "String",required = true,paramType = "body"),
            @ApiImplicitParam(name = "chemicalsEn", value = "化学品英文名称",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsAlias", value = "化学品别名",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsCas", value = "cas",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsType", value = "化学品类别",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "chemicalsMsds", value = "化学品文件",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "supplierName", value = "供应商名称",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "supplierContactName", value = "供应商联系人",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "supplierContactPhone", value = "供应商联系方式",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "emConsultantName", value = "咨询单位名称",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "emContactName", value = "应急联系人",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "emContactPhone", value = "应急联系方式",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "storageArea", value = "仓库区域",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "useArea", value = "使用区域",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "isDangerousGood", value = "是否是危险品",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "dangerousCategory", value = "危险类型",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "registrationNo", value = "登记号",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "packingCategory", value = "包装类别",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "hazardCharacteristics", value = "危险特性",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "activeHazard", value = "活性危害",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "prevention", value = "预防及应急措施",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "hazardNo", value = "危规号",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "incompatibility", value = "禁配物",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "stockCount", value = "库存量",dataType = "String",paramType = "body")

    })
    public ResponseEntity<Object> update(@Validated @RequestBody Chemicla resources){
        chemiclaService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除化学品管理")
    @ApiOperation("删除化学品管理")
    @PreAuthorize("@el.check('chemicla:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        chemiclaService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}